#coding:utf-8

#数乘与矩阵相乘
from Matrix import Matrix

def calValue(m1,m2,i,j):
    n = m1.col
    arr = [ m1[i][r]*m2[r][j] for r in xrange(0,n) ]
    res = sum(arr)
    if abs(res - round(res))<1e10:
        res = round(res)
    return res

def _mul(this,m2):
    if isinstance(m2,Matrix):
        if this.col != m2.row:
            raise ValueError('两个矩阵的行列不匹配!')

        new_matrix = [
            [
                calValue(this,m2,i,j) for j in xrange(0,m2.col)
            ] for i in xrange(0,this.row)
        ]

        return Matrix(*new_matrix)

    else:
        new_matrix = [
            [
                this[i][j]*m2 for j in xrange(0,this.col)
            ] for i in xrange(0,this.row)
        ]

        return Matrix(*new_matrix)

    